<?php
/**
 * Author:   songsong
 * Email:    747149759@qq.com
 * Date:     2019/3/12 0012
 * Describe: 银行卡解绑
 */
namespace app\index\controller;
use think\Controller;
use think\Db;
use think\Session;
use think\Request;
use changpay\ChangPay;
class Bank extends Base
{
    /**
     * 银行卡解绑
     * @param string $str 协议名称
     * @param int $userid 用户ID
     * @param int $orderid 订单ID
     * @return array
     */
 
 
//	 银行卡解除绑定操作
	public function jibang_bank(){
	    $header =Request::instance()->header();
		$data=Db::table('xb_renzen_bank')->where(['UserID'=>$header['userid']])->find();
		$changpay= new \changpay\ChangPay;
		$data['Status']='2';
		$dataxiuga['isBank']='0';
		$data['UpdateTime']=date('Y-m-d H:i:s');//时间
		$re=Db::table('xb_renzen_bank')->update($data);
		$UserID=Db::table('xb_renzen_bank')->where(['ID'=>$data['ID']])->value('UserID');//获取UserID
		$res=Db::table('xb_mem_info')->where(['ID'=>$UserID])->update($dataxiuga);//修改用户表身份证认证状态
		$MerchantNo=Db::table('xb_sys_inteparameter')->where('ParaName','changpay')->value('ParaValue');
//		$MerchantNo = '200002080023' ; //商户号码
		$ressss=$changpay->jiebang($data['BankNo'],$UserID,$MerchantNo);
//		p($ressss);die;
		if($ressss){
	    show_success_api();
		}else{
			show_error_api();
		}	
	}
	//	 获取银行卡信息  （用于展示用户银行卡信息）
	public function bank_datail(){
	    $header =Request::instance()->header();
	    $re=Db::table('xb_renzen_bank')->where(['UserID'=>$header['userid']])->find();
		if(!$re){
			show_error_apio('20001');
		}
		$data=Db::table('xb_mem_info a')->
		field('a.TrueName,b.BankNo,b.BankName')
		->where(['UserID'=>$header['userid']])
		->join('xb_renzen_bank b','a.ID=b.UserID','LEFT')
		->find();
		if($data){
	    show_success_apio($data);
		}else{
			show_error_apio('10001');
		}	
	}
//	根据银行卡号获取银行卡信息
	public function bank_bankno()
    {
		$post = $this->request->param(true);
		if(empty($post['bankNo'])){
			 show_error_api('银行卡号不能为空');
		}
		$changpay= new \changpay\ChangPay;
		 $datas=$changpay->bank_datile($post['bankNo']);
		 if($datas['AcceptStatus']=='S'){
			if($datas['IsValid']=='是'){
	          show_success_api($datas);
			}else{
				 show_error_api('请输入正确的银行卡号');
			}
		 }else{
		 	  show_error_api($datas['OriginalErrorMessage']);
		 }
	}

    /**
     * 获取支付方式
     * @param int $userid 用户ID
     * @return array
     */
    public function getPayMode()
    {
        //获取header请求参数的用户ID
        $header = Request::instance()->header();

        //获取开启的支付方式并返回接口信息
        $temp = $this->getBankModel($header['userid']);
        if($temp){
            returnSuccessResult($temp);
        }else{
            returnErrorResult('支付方式都关闭了');
        }
    }

    /**
     * 获取用户银行卡信息
     * @param int $userId 用户ID
     * @return array
     */
    public function getUserBank($userId)
    {
        return db('renzen_bank')->where(['UserID' => $userId])->find();
    }

    /**
     * 获取可支付的支付方式
     * @param int $userId 用户ID
     * @return array
     */
    public function getBankModel($userId)
    {
        $systemInfo = (new System())->getSystemInfo();

        $temp = [];

        if($systemInfo['isAlipay'] == 1){
            $temp[] = [
                'payType' => 1,
                'name'    => '支付宝',
            ];
        };
        if($systemInfo['isWx'] == 1){
            $temp[] = [
                'payType' => 2,
                'name'    => '微信',
            ];
        };
        if($systemInfo['isBank'] == 1){
            $userInfo = $this->getUserBank($userId);
            $bankSuffix = substr($userInfo['BankNo'],-4);
            $temp[] = [
                'payType' => 3,
                'name'    => $userInfo['BankName'],
                'BankNo'  => $bankSuffix,
            ];
        };

        return $temp;
    }
}